#include "generateRoundKeys.h"
#include "sBoxLayer.h"
#include "bits.h"
#include <cstring>
using namespace std;

void shiftKey(unsigned char *dst) {
	unsigned char src[10];
	memcpy(src, dst, sizeof(char[10]));
	
	for(int i = 79; i >= 0; --i) {
		if(bitArrayGet(src, (i + 61) % 80) > 0)
			bitArraySet(dst, i);
		else
			bitArrayClear(dst, i);
	}
}

void generateRoundKeys(unsigned char *key, unsigned char round_keys[][8]) {
	unsigned char current_key[10];
	memcpy(current_key, key, sizeof(char[10]));
	
	for(unsigned char round_counter=0; round_counter<32; ++round_counter) {
		shiftKey(key);
		// S-Box Leftmost bits
		key[0] = present_sbox[key[0] & 0xF0] | (key[0] & 0x0F);
		// XOR round count
		key[7] ^= (round_counter & 0x1E) >> 1;
		key[8] ^= (round_counter & 0x01) << 7;
		
		// Save new round key
		memcpy(round_keys[round_counter], current_key, sizeof(char[8]));
	}
}
